/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.Bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.Modelo.Autor;

/**
 *
 * @author Yessy
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    //llamada a EJB

    @EJB
    private AutorBlLocal autorBl;
    //variables locales
    private List<Autor> lista;
    private Autor author;
    private String accion;

    /**
     * Creates a new instance of AuthorBean
     */
    public AuthorBean() {
        author = new Autor();
    }
    //Get & Set

    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }

    public List<Autor> getLista() {
        lista = autorBl.getAll();
        return lista;
    }

////     //Acciones 
////      public String prepararNuevoAutor(){
////          author=new Autor();
////      return "AutorCrearEditar";
////      }
    public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        autorBl.Register(author);
                }else{
        if(accion.equalsIgnoreCase("Editar")){
        autorBl.modify(author);
        }
        else{
        if(accion.equalsIgnoreCase("Eliminar"))
            autorBl.delete(author);
        }
        }
        
        
        //autorBl.Register(author);

        return "AutorLista";
    }

    //Listeners
    public void prepararNuevoRegistro(ActionEvent evt) {
        accion="Nuevo";
        author = new Autor();
    }
   public void prepararEditar(ActionEvent evt) {
       accion="Editar";
      System.out.println(evt.getComponent().getClientId());        
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        author=lista.get(index);
    } 
   public void prepararEliminar(ActionEvent evt){
   accion="Eliminar";
   System.out.println(evt.getComponent().getClientId());
    int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        author=lista.get(index);
   
   }
}
